//
//  PCDSyncCustomPlugin.m
//  PCDBank
//
//  Created by DK on 2021/2/26.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDSyncCustomPlugin.h"
#import "PCDDataBaseModel.h"
#import "PCDPageViewMange.h"
#import "PCDBankWebViewController.h"
#import "PCDLoginSession.h"

#define SDKVersion @"6.1"

@implementation PCDSyncCustomPlugin

-(NSString *)ares_syncfunction:(id)data {
    return @"";
}

-(NSString *)ares_getStorageInfoSync:(id)data {

    return [NSString stringWithFormat:@"%@",[[PCDDataBaseModel shareModel] getStorageInfoFromDataBase]];
}


- (NSString *)ares_setStorageSync:(id)data {
    
    if (1) {
        NSString *keyString = [data objectForKey:@"key"];
        id dataInfo = [data objectForKey:@"data"];
        //数据持久化 应用模式使用
        BOOL lasted = [[data objectForKey:@"lasted"] boolValue];
        if (lasted) {
            //需要持久化则删除内存中的数据
            [[PCDDataBaseModel shareModel].memoryStorage removeObjectForKey:keyString];
            //            dataInfo = [[PCMPDataBaseModel shareModel] setStorageSyncToDataBaseWith:data];
            [[PCDDataBaseModel shareModel] setStorageToDataBaseWith:data];
        } else {
            //不需要持久化则删除持久化的数据
            NSString *dataString = @"";
            if ( [dataInfo isKindOfClass:[NSString class]]) {
                dataString = dataInfo;
            } else if ( [dataInfo isKindOfClass:[NSDictionary class]]||[dataInfo isKindOfClass:[NSArray class]]) {
                dataString = [dataInfo JSONRepresentation];
            }  else if ( [dataInfo isKindOfClass:[NSData class]]) {
                dataString = [[NSString alloc] initWithData:dataInfo encoding:NSUTF8StringEncoding];
            }
            [[PCDDataBaseModel shareModel].memoryStorage setObject:dataString forKey:keyString];
            
            //            [[PCDDataBaseModel shareModel].memoryStorage setObject:dataInfo forKey:keyString];
            [[PCDDataBaseModel shareModel] removeStorageFromDataBaseWith:data];
        }
        return @"";
    }
    
    return [[PCDDataBaseModel shareModel] setStorageToDataBaseWith:data];
}

- (NSString *)ares_getStorageSync:(id)data
{
    PCDLoginSession *session = [PCDUtil loginSession];
    if (session) {
        NSString *strSession = [session toString];
        [[PCDDataBaseModel shareModel].memoryStorage setValue:strSession forKey:kPCDLoginSessionKey];
    }
    NSDictionary *parameters = (NSDictionary *)data;
    
    NSString *keyString = [parameters objectForKey:@"key"];
    id memoryDataInfo = [[PCDDataBaseModel shareModel].memoryStorage objectForKey:keyString];
    id dataInfo = nil;
    if (memoryDataInfo) {
        dataInfo = memoryDataInfo;
    } else {
        dataInfo = [[PCDDataBaseModel shareModel] getStorageFromDataBaseWith:parameters];
    }
    
    if (![dataInfo isKindOfClass:[NSString class]]) {
        dataInfo = [dataInfo JSONRepresentation];
    }
    
    return dataInfo;

}

- (NSString *)ares_removeStorageSync:(id)data {
    //FIXME:应用模式 内存和数据库中的数据都清空
    if (1) {
        //删除内存中的数据
        NSString *keyString = [data objectForKey:@"key"];
        if (![PCDUtil nilOrEmptyString:keyString]) {
            if ([[[PCDDataBaseModel shareModel].memoryStorage allKeys] containsObject:keyString]) {
                [[PCDDataBaseModel shareModel].memoryStorage removeObjectForKey:keyString];
            }
        }
    }
    return [[PCDDataBaseModel shareModel] removeStorageFromDataBaseWith:data];
}

- (NSString *)ares_clearStorageSync:(id)data {
    if (1) {
        //不清空session
        if ([[[PCDDataBaseModel shareModel].memoryStorage allKeys] containsObject:kPCDLoginSessionKey]) {
            NSString *loginSession = [[PCDDataBaseModel shareModel].memoryStorage objectForKey:kPCDLoginSessionKey];
            [[PCDDataBaseModel shareModel].memoryStorage removeAllObjects];
            [[PCDDataBaseModel shareModel].memoryStorage setObject:loginSession forKey:kPCDLoginSessionKey];
        } else {
            [[PCDDataBaseModel shareModel].memoryStorage removeAllObjects];
        }
    }
    [[PCDDataBaseModel shareModel] clearStorageFromDataBase];
    return @"";
}

- (NSString *)ares_getSystemInfoSync:(id)data {
    NSDictionary *params = @{@"SDKVersion":SDKVersion,@"version":[PCDUtil appVersion]};
    return [params JSONRepresentation];
}

- (NSString *)ares_getCurrentPagesSync:(id)data {
    
    NSArray *array =PCDPageViewMangeGet().baseNavgatView.viewControllers;
    int nCount =0;
    for (int i=0;i<array.count;i++) {
       
        UIViewController *tempView = [array objectAtIndex:i];
        if ([tempView isKindOfClass:[PCDBankWebViewController class]]) {
            nCount ++;
        }
    }
    NSString *strCount =[NSString stringWithFormat:@"%d",nCount];
    NSDictionary *params = @{@"number":strCount};
    return [params JSONRepresentation];
}

@end
